<template>
  <view class="weather-page">
    <!-- 页面标题 -->
    <view class="page-title">明天天气预报</view>

    <!-- 操作按钮 -->
    <button class="get-weather-btn" @click="getWeatherData">获取天气预报</button>

    <!-- 加载状态提示 -->
    <view class="loading-tip" v-if="isLoading">正在获取天气数据，请稍候...</view>

    <!-- 错误提示 -->
    <view class="error-tip" v-if="errorMsg">
      <text class="error-text">{{ errorMsg }}</text>
      <button class="retry-btn" @click="getWeatherData">重试</button>
    </view>

    <!-- 天气信息展示区 -->
    <view class="weather-info" v-if="tomorrowWeather">
      <view class="info-item">
        <text class="item-label">日期：</text>
        <text class="item-value">{{ tomorrowWeather.date }}</text>
      </view>
      <view class="info-item">
        <text class="item-label">星期：</text>
        <text class="item-value">{{ tomorrowWeather.week }}</text>
      </view>
      <view class="info-item">
        <text class="item-label">最高温：</text>
        <text class="item-value">{{ tomorrowWeather.high }}</text>
      </view>
      <view class="info-item">
        <text class="item-label">最低温：</text>
        <text class="item-value">{{ tomorrowWeather.low }}</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false, 
      errorMsg: '', 
      tomorrowWeather: null, 
      weatherApi: 'http://t.weather.sojson.com/api/weather/city/101230501' 
    };
  },
  methods: {
    getWeatherData() {
      this.isLoading = true;
      this.errorMsg = '';
      this.tomorrowWeather = null;

      uni.request({
        url: this.weatherApi,
        method: 'GET',
        timeout: 60000,
        success: (res) => {
          if (res.statusCode === 200 && res.data?.data?.forecast) {
            const tomorrow = res.data.data.forecast[1];
            if (tomorrow) {
              this.tomorrowWeather = {
                date: tomorrow.date,
                week: tomorrow.week,
                high: tomorrow.high,
                low: tomorrow.low
              };
            } else {
              this.errorMsg = '未获取到明天的天气数据';
            }
          } else {
            this.errorMsg = '接口返回数据异常，请稍后重试';
          }
        },
        fail: (err) => {
          this.errorMsg = '网络请求失败，请检查网络连接';
          console.error('请求失败：', err);
        },
        complete: () => {
          this.isLoading = false;
        }
      });
    }
  }
};
</script>

<style scoped>
.weather-page {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 30rpx 20rpx;
}

.page-title {
  font-size: 20rpx;
  font-weight: 500;
  color: #333;
  text-align: center;
  margin-bottom: 50rpx;
}

.get-weather-btn {
  width: 400rpx;
  height: 80rpx;
  line-height: 80rpx;
  margin: 0 auto 40rpx;
  background-color: #007aff;
  color: #fff;
  border-radius: 12rpx;
}

.loading-tip {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-top: 40rpx;
}

.error-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 40rpx;
}

.error-text {
  color: #ff4d4f;
  font-size: 14px;
  margin-bottom: 20rpx;
}

.retry-btn {
  width: 150rpx;
  height: 60rpx;
  line-height: 60rpx;
  background-color: #007aff;
  color: #fff;
  border-radius: 8rpx;
  font-size: 12px;
}

.weather-info {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-top: 40rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.info-item {
  display: flex;
  padding: 18rpx 0;
  border-bottom: 2px solid #f0f0f0;
  font-size: 14px;
}

.info-item:last-child {
  border-bottom: none;
}

.item-label {
  color: black;
  flex: 0 0 120rpx;
}

.item-value {
  color: #333;
  flex: 1;
  font-weight: 400;
}
</style>